package top.juntower.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@EnableOpenApi
public class WebConfig implements WebMvcConfigurer {
    //设置跨域
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 用户证书验证，
        registry.addMapping("/**").allowedOrigins("*").allowedMethods("POST", "GET", "DELETE", "PUT", "HEAD", "OPTIONS")
                .allowCredentials(false)
                .allowedHeaders(
                        "Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Pragma")
                .exposedHeaders(
                        "Access-Control-Allow-Origin,Access-Control-Allow-Headers,Access-Control-Allow-Credentials,Pragma")
                // 最大过期时间
                .maxAge(3600);
    }
        //配置swagger的bean实例
        @Bean
        public Docket docket () {
            return new Docket(DocumentationType.OAS_30)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("top.juntower.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
        //配置swagger信息
        private ApiInfo apiInfo () {
            //作者的信息
            Contact contact = new Contact("美食能改变一个人", "http://juntawer.top/", "2314755664@qq.com");
            return new ApiInfoBuilder()
                    .title("美食网站后台api文档")
                    .description("美食网站的后端api")
                    .contact(contact)
                    .version("v1.0")
                    .build();

        }

    }
